package com.netty.demo.echo;

import com.netty.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.List;

/**
 * Echo Server Handler.
 *
 * @author <a href="https://waylau.com">Way Lau</a>
 * @since 1.0.0 2019年10月2日
 */
@ChannelHandler.Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    private byte[] plcHead1_200smart = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 16, 0, -62, 2, 3, 0, -64, 1, 10};
    private byte[] plcHead2_200smart = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, -52, -63, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};
//    private byte[] plcHead1_200 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -63, 2, 77, 87, -62, 2, 77, 87, -64, 1, 9};
//    private byte[] plcHead2_200 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 0, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 3, -64};

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        byte length = 0;
        if (msg instanceof List) {
            List list = (List) msg;
            if (list.size() > 30)
                System.out.println("23--->" + list.get(23) + " 24--->" + list.get(24));
        }
        System.out.println(ctx.channel().remoteAddress() + " -> Server :" + msg);
        byte[] HeadBytes = new byte[31];
        HeadBytes[0] = 3;
        HeadBytes[1] = 0;
        HeadBytes[2] = 0;
        HeadBytes[3] = 31;
        HeadBytes[20] = 1;
        HeadBytes[21] = -1;
        //bool
        HeadBytes[22] = 3;
        //read
        HeadBytes[23] = -1;
        HeadBytes[24] = 4;


        HeadBytes[25] = 1;

        HeadBytes[27] = 1;
        HeadBytes[28] = 1;
        HeadBytes[29] = 0;
        HeadBytes[30] = 0;
        ByteBuf byteBuf = Unpooled.buffer();
        byteBuf.writeBytes(HeadBytes);
        ctx.channel().writeAndFlush(byteBuf);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }

    public static void main(String[] args) {
//        byte[] HeadBytes = new byte[4];
//        HeadBytes[0] = 3;
//        HeadBytes[1] = 0;
//        HeadBytes[2] = 0;
//        HeadBytes[3] = 8;
//        ByteBuf byteBuf = Unpooled.buffer();
//        byteBuf.writeBytes(HeadBytes);
//        byteBuf.writeInt(99);
//        int i =byteBuf.readableBytes();
//        while (i>0){
//            i--;
//            System.out.println(byteBuf.readByte());
//        }
        short[] length = new short[1];
        length[0] = 4;
        int receiveCount = 4;
        byte[] Content = new byte[29];
        Content[20] = 1;
        Content[21] = -1;
        Content[22] = 3;

        Content[25] = 1;
        Content[26] = 0;
        Content[27] = 0;
        Content[28] = 4;
        if (Content.length >= 21 && Content[20] == length.length) {
            byte[] buffer = new byte[receiveCount];
            int kk = 0;
            int ll = 0;

            for (int ii = 21; ii < Content.length; ++ii) {
                if (ii + 1 < Content.length && Content[ii] == -1 && Content[ii + 1] == 4) {
                    System.arraycopy(Content, ii + 4, buffer, ll, length[kk]);
                    ii += length[kk] + 3;
                    ll += length[kk];
                    ++kk;
                }
            }

            for (byte b : buffer) {
                System.out.println(b);
            }
        }
    }

    public void dd() {
        short[] length = new short[1];
        int readCount = length.length;
        byte[] _PLCCommand = new byte[19 + readCount * 12];
        _PLCCommand[0] = 3;
        _PLCCommand[1] = 0;
        _PLCCommand[2] = (byte) (_PLCCommand.length / 256);
        _PLCCommand[3] = (byte) (_PLCCommand.length % 256);
        _PLCCommand[4] = 2;
        _PLCCommand[5] = -16;
        _PLCCommand[6] = -128;
        _PLCCommand[7] = 50;
        _PLCCommand[8] = 1;
        _PLCCommand[9] = 0;
        _PLCCommand[10] = 0;
        _PLCCommand[11] = 0;
        _PLCCommand[12] = 1;
        _PLCCommand[13] = (byte) ((_PLCCommand.length - 17) / 256);
        _PLCCommand[14] = (byte) ((_PLCCommand.length - 17) % 256);
        _PLCCommand[15] = 0;
        _PLCCommand[16] = 0;
        _PLCCommand[17] = 4;
        _PLCCommand[18] = (byte) readCount;

        for (int ii = 0; ii < readCount; ++ii) {
            _PLCCommand[19 + ii * 12] = 18;
            _PLCCommand[20 + ii * 12] = 10;
            _PLCCommand[21 + ii * 12] = 16;
            _PLCCommand[22 + ii * 12] = 2;
            _PLCCommand[23 + ii * 12] = (byte) (length[ii] / 256);
            _PLCCommand[24 + ii * 12] = (byte) (length[ii] % 256);
//            _PLCCommand[25 + ii * 12] = (byte)((Integer)address[ii].Content3 / 256);
//            _PLCCommand[26 + ii * 12] = (byte)((Integer)address[ii].Content3 % 256);
//            _PLCCommand[27 + ii * 12] = (Byte)address[ii].Content1;
//            _PLCCommand[28 + ii * 12] = (byte)((Integer)address[ii].Content2 / 256 / 256 % 256);
//            _PLCCommand[29 + ii * 12] = (byte)((Integer)address[ii].Content2 / 256 % 256);
//            _PLCCommand[30 + ii * 12] = (byte)((Integer)address[ii].Content2 % 256);
        }
    }

}